package com.doudou.casebook.utils;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;

import com.doudou.casebook.R;
import com.doudou.casebook.adapter.CalendarAdapter;
import com.doudou.casebook.db.DBManager;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

public class CalendarDialog extends Dialog implements View.OnClickListener {
    ImageView errorIv;
    GridView gv;
    LinearLayout hsvlayout;


    List<TextView> hsvViewList;
    List<Integer> yearList;

    int selelectPos = -1;//表示当前选中年份的位置
    int selelectMonth = -1;//表示当前选中月份的位置
    private CalendarAdapter adapter;
    OnRefreshListhener onRefreshListhener;

    public interface OnRefreshListhener {
        public void onRefrensh(int selPos,int position, int year, int month);
    }

    public void setOnRefreshListhener(OnRefreshListhener onRefreshListhener) {
        this.onRefreshListhener = onRefreshListhener;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_calendar);
        gv = findViewById(R.id.dialog_calender_gv);
        errorIv = findViewById(R.id.dialog_calender_iv);
        hsvlayout = findViewById(R.id.dialog_calender_layout);

        errorIv.setOnClickListener(this);

        //向横向的scrollView当中添加View的方法
        if (addViewToLayout()) {
            initGridView();
            //设置gridview中的每个item点击事件
            setGVListhener();

        }


    }

    private void setGVListhener() {
        gv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                adapter.selPos = position;
                adapter.notifyDataSetInvalidated();
                //获取到被选中的年份和月份
                if (onRefreshListhener != null) {
                    int year = adapter.year;
                    String month = adapter.getItem(position).toString();

                    onRefreshListhener.onRefrensh(selelectPos,position, year, Integer.parseInt(month));
                }
                cancel();
            }
        });
    }

    private void initGridView() {
        int year = yearList.get(selelectPos);
        adapter = new CalendarAdapter(getContext(), year);
        if (selelectMonth == -1) {

            adapter.selPos = 0;
        }else{
            adapter.selPos=selelectMonth;
        }
        gv.setAdapter(adapter);
    }

    private boolean addViewToLayout() {
        hsvViewList = new ArrayList<>();
        yearList = DBManager.getYearListFromAccounttb();
        if (yearList.size() == 0) {
            //没有记录
            TextView noDataTips = findViewById(R.id.dialog_calender_no_data);
            noDataTips.setVisibility(View.VISIBLE);
            return false;
        } else {
            //遍历年份,有几年,就向scrollView添加几个view
            for (int i = 0; i < yearList.size(); i++) {
                Integer year = yearList.get(i);
                View view = getLayoutInflater().inflate(R.layout.item_dialogcal_hsv, null);
                hsvlayout.addView(view);
                TextView hsvTv = view.findViewById(R.id.item_dialogcal_hsv_tv);
                ;
                hsvTv.setText(year + "");
                hsvViewList.add(hsvTv);
            }
            if (selelectPos == -1) {
                selelectPos = hsvViewList.size() - 1;//设置当前选中的是最近的年份
            }
            changeTvbg(selelectPos);//设置选中状态
            setHSVClickListener();//设置每个view的监听事件

            return true;
        }


    }

    private void setHSVClickListener() {
        for (int i = 0; i < hsvViewList.size(); i++) {
            TextView textView = hsvViewList.get(i);
            int pos = i;
            textView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    changeTvbg(pos);
                    selelectPos = pos;
                    //获取被选中的年份,下面的GridView的数据源会发生变化
                    Integer year = yearList.get(selelectPos);
                    adapter.setYear(year);
                }
            });
        }
    }
//传入被选中的位置,改变此位置上的背景颜色


    @SuppressLint("ResourceAsColor")
    private void changeTvbg(int slelectPos) {
        for (int i = 0; i < hsvViewList.size(); i++) {
            TextView textView = hsvViewList.get(i);
            textView.setBackgroundResource(R.drawable.dialog_btn_bg);
            textView.setTextColor(R.color.colorPrimary);
        }
        TextView selectTextView = hsvViewList.get(slelectPos);
        selectTextView.setBackgroundResource(R.drawable.main_recordbtn_bg);
        selectTextView.setTextColor(Color.WHITE);
    }

    public CalendarDialog(@NonNull Context context,int selelectPos,int selelectMonth) {
        super(context);
        this.selelectPos = selelectPos;
        this.selelectMonth=selelectMonth;


    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.dialog_calender_iv) {
            cancel();
        } else if (v.getId() == R.id.dialog_calender_gv) {
            Toast.makeText(getContext(), "gv", Toast.LENGTH_SHORT).show();
        } else if (v.getId() == R.id.dialog_calender_layout) {
            Toast.makeText(getContext(), "layout", Toast.LENGTH_SHORT).show();
        }
    }

    //设置dialog尺寸与屏幕尺寸一样宽
    public void setDialogSize() {
        //获取窗口对象
        Window window = getWindow();
        //获取对象参数
        assert window != null;
        WindowManager.LayoutParams wlp = window.getAttributes();
        //获取屏幕宽度
        Display d = window.getWindowManager().getDefaultDisplay();
        wlp.width = d.getWidth();
        wlp.gravity = Gravity.TOP;
        window.setBackgroundDrawableResource(android.R.color.transparent);
        window.setAttributes(wlp);

    }
}
